const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    inputValue:'',
    lists:[],
    page:1,
    pageSize:10,
    isEnd:false,
    isloading:true
  },
  onLoad: function (options) {
    if(options.search){
      this.setData({
        inputValue:options.search
      })
    }
    this.search()
  },
  search(){
    var data={
      keyword:this.data.inputValue,
      isHot:'',
      page:this.data.page,
      pageSize:this.data.pageSize,
      companyId:'',
      city:''
    }
    Controller.joblist(data).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        this.setData({
          lists:this.data.lists.concat(res.data.baseData.list),
        })
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })
    })

  },
  cancel(){
    this.setData({
      inputValue:'',
      lists:[],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.search()
  },
  golink(e){
    if(!wx.getStorageSync('logintoken')){
      app.goLogin()
      return
    }
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  
  searchSubmit(e){
    console.log(e)
    let _SearchValue = this.data.inputValue;
    let localStorageValue = [];

    if (_SearchValue != '') {

        //添加搜索历史记录
        //调用API从本地缓存中获取数据
        localStorageValue = wx.getStorageSync('searchData') || [];

        let tempSearchData = [];
        //过滤历史列表中相同的搜索记录
        for (let item of localStorageValue) {
            if (item != _SearchValue) {
                tempSearchData.push(item);
            }
        }
        //添加搜索文本进历史记录
        tempSearchData.push(_SearchValue);
        //保存至从本地缓存
        wx.setStorageSync('searchData', tempSearchData);
        
    }
    this.setData({
      lists:[],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.search()
  },
  bindInput(e){
    var value=e.detail.value || ''

      this.setData({
        inputValue: value
      })
 
  },
  onShareAppMessage: function () {

  }
})